<% unless goal.nil? %>
  <% experiment_class = "experiment experiment_with_goal" %>
<% else %>
  <% experiment_class = "experiment" %>
<% end %>

<% experiment.calc_winning_alternatives %>
<%
  extra_columns = []
  experiment.alternatives.each do |alternative|
    extra_info = alternative.extra_info || {}
    extra_columns += extra_info.keys
  end

  extra_columns.uniq!
  summary_texts = {}
  extra_columns.each do |column|
    extra_infos = experiment.alternatives.map(&:extra_info).select{|extra_info| extra_info && extra_info[column] }

    if extra_infos.length > 0 && extra_infos.all? { |extra_info| extra_info[column].kind_of?(Numeric) }
      summary_texts[column] = extra_infos.inject(0){|sum, extra_info| sum += extra_info[column]}
    else
      summary_texts[column] = "N/A"
    end
  end
%>


<div class="<%= experiment_class %>" data-name="<%= experiment.name %>" data-complete="<%= experiment.has_winner? %>">
  <div class="experiment-header">
    <h2>
      Experiment: <%= experiment.name %>
      <% if experiment.version > 1 %><span class='version'>v<%= experiment.version %></span><% end %>
      <% unless goal.nil? %><span class='goal'>Goal:<%= goal %></span><% end %>
      <% metrics = @metrics.select {|metric| metric.experiments.include? experiment} %>
      <% unless metrics.empty? %>
        <span class='goal'>Metrics:<%= metrics.map(&:name).join(', ') %></span>
      <% end %>
    </h2>

    <% if goal.nil? %>
      <div class='inline-controls'>
        <small><%= experiment.start_time ? experiment.start_time.strftime('%Y-%m-%d') : 'Unknown' %></small>
        <%= erb :_controls, :locals => {:experiment => experiment} %>
      </div>
    <% end %>
  </div>
  <table>
    <tr>
      <th>Alternative Name</th>
      <th>Participants</th>
      <th>Non-finished</th>
      <th>Completed</th>
      <th>Conversion Rate</th>
      <% extra_columns.each do |column| %>
        <th><%= column %></th>
      <% end %>
      <th>
        <form>
          <select id="dropdown-<%=experiment.jstring(goal)%>" name="dropdown-<%=experiment.jstring(goal)%>">
            <option value="confidence-<%=experiment.jstring(goal)%>">Confidence</option>
            <option value="probability-<%=experiment.jstring(goal)%>">Probability of being Winner</option>
          </select>
        </form>
      </th>
      <th>Finish</th>
    </tr>

    <% total_participants = total_completed = total_unfinished = 0 %>
    <% experiment.alternatives.each do |alternative| %>
      <tr>
        <td>
          <%= alternative.name %>
          <% if alternative.control? %>
            <em>control</em>
          <% end %>
          <form action="<%= url('force_alternative') + '?experiment=' + experiment.name %>" method='post'>
            <input type='hidden' name='alternative' value='<%= h alternative.name %>'>
            <input type="submit" value="Force for current user" class="green">
          </form>
        </td>
        <td><%= alternative.participant_count %></td>
        <td><%= alternative.unfinished_count %></td>
        <td><%= alternative.completed_count(goal) %></td>
        <td>
          <%= number_to_percentage(alternative.conversion_rate(goal)) %>%
          <% if experiment.control.conversion_rate(goal) > 0 && !alternative.control? %>
            <% if alternative.conversion_rate(goal) > experiment.control.conversion_rate(goal) %>
              <span class='better'>
                +<%= number_to_percentage((alternative.conversion_rate(goal)/experiment.control.conversion_rate(goal))-1) %>%
              </span>
            <% elsif alternative.conversion_rate(goal) < experiment.control.conversion_rate(goal) %>
              <span class='worse'>
                <%= number_to_percentage((alternative.conversion_rate(goal)/experiment.control.conversion_rate(goal))-1) %>%
              </span>
            <% end  %>
          <% end %>
        </td>
        <script type="text/javascript" id="sourcecode">
          $(document).ready(function(){
            $('.probability-<%=experiment.jstring(goal)%>').hide();
            $('#dropdown-<%=experiment.jstring(goal)%>').change(function() {
              $('.box-<%=experiment.jstring(goal)%>').hide();
              $('.' + $(this).val()).show();
            });
          });
        </script>
        <% extra_columns.each do |column| %>
          <td><%= alternative.extra_info && alternative.extra_info[column] %></td>
        <% end %>
        <td>
          <div class="box-<%=experiment.jstring(goal)%> confidence-<%=experiment.jstring(goal)%>">
            <span title='z-score: <%= round(alternative.z_score(goal), 3) %>'><%= confidence_level(alternative.z_score(goal)) %></span>
            <br>
          </div>
          <div class="box-<%=experiment.jstring(goal)%> probability-<%=experiment.jstring(goal)%>">
            <span title="p_winner: <%= round(alternative.p_winner(goal), 3) %>"><%= number_to_percentage(round(alternative.p_winner(goal), 3)) %>%</span>
          </div>
        </td>
        <td>
          <% if experiment.has_winner? %>
            <% if experiment.winner.name == alternative.name %>
              Winner
            <% else %>
              Loser
            <% end %>
          <% else %>
            <form action="<%= url('experiment') + '?experiment=' + experiment.name %>" method='post' onclick="return confirmWinner()">
              <input type='hidden' name='alternative' value='<%= h alternative.name %>'>
              <input type="submit" value="Use this" class="green">
            </form>
          <% end %>
        </td>
      </tr>

      <% total_participants += alternative.participant_count %>
      <% total_unfinished += alternative.unfinished_count %>
      <% total_completed += alternative.completed_count(goal) %>
    <% end %>

    <tr class="totals">
      <td>Totals</td>
      <td><%= total_participants %></td>
      <td><%= total_unfinished %></td>
      <td><%= total_completed %></td>
      <td>N/A</td>
      <% extra_columns.each do |column| %>
        <td>
          <%= summary_texts[column] %>
        </td>
      <% end %>
      <td>N/A</td>
      <td>N/A</td>
    </tr>
  </table>
</div>
